/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pAddTransaction.java
 *
 * Created on Mar 4, 2011, 10:09:08 PM
 */

package com.fa.supporters;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import java.util.Vector;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

/**
 *
 * @author tatung
 */
public class pAddTransaction extends javax.swing.JPanel {
    private boolean isOutTransaction ;

    private String date;
    private String partnerName;
    private String ProductName;
    private float quantity;
    private String metric;
    private String note;

    
    Vector<String> strDateVector = new Vector<String>();
    /** Creates new form pAddTransaction */
    public pAddTransaction(boolean isOutTransaction, String partnerName) {
        initComponents();
        this.partnerName = partnerName;

        this.isOutTransaction = isOutTransaction;
        DefaultComboBoxModel dcbm = new DefaultComboBoxModel(DbConnector.getPartnerList());
        this.cbxPartnerName.setModel(dcbm);
        this.cbxPartnerName.setSelectedItem(partnerName);

        tblTransaction.setModel(new AddTransactionTableModel());
        
        this.dchrDate.setDateFormatString("yyyy-MM-dd");
        this.dchrDate.setDate(Calendar.getInstance().getTime());
        
        SpinnerDateModel sm = new SpinnerDateModel(new Date(), null, null, Calendar.HOUR_OF_DAY);
        this.spnTime.setModel(sm);
        JSpinner.DateEditor de = new JSpinner.DateEditor(spnTime, "HH:mm");
        spnTime.setEditor(de);


        //product list combobox
        JComboBox cbxProduct = new JComboBox(new DefaultComboBoxModel(DbConnector.getProductList(isOutTransaction)));
        cbxProduct.setEditable(false);
        DefaultCellEditor productCbxDce = new DefaultCellEditor(cbxProduct);
        tblTransaction.getColumnModel().getColumn(0).setCellEditor(productCbxDce);

        //metric combobox
        JComboBox cbxMetric = new JComboBox(new DefaultComboBoxModel(DbConnector.getMetricList()));
        cbxMetric.setEditable(false);
        DefaultCellEditor metricCbxDce = new DefaultCellEditor(cbxMetric);
        tblTransaction.getColumnModel().getColumn(2).setCellEditor(metricCbxDce);
        
        tblTransaction.setRowHeight(25);
    }

    public boolean isOutTransaction(){
        return isOutTransaction;
    }
    public String getDateTime(){
        String ret = (new SimpleDateFormat("yyyy-MM-dd")).format(this.dchrDate.getDate()).toString();
        ret += " " + (new SimpleDateFormat("HH:mm").format(this.spnTime.getValue()).toString()) + ":00";
        return ret;
    }
    public String getPartnerName(){
        return (String)this.cbxPartnerName.getSelectedItem();
    }
    public String getProductName(){
        return ProductName;
    }
    public float getQuantity(){
        return quantity;
    }
    public String getMetric(){
        return metric;
    }
    public String getNote(){
        return note;
    }

    public Object[][] getData(){
        return ((AddTransactionTableModel)this.tblTransaction.getModel()).getData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cbxPartnerName = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTransaction = new javax.swing.JTable();
        btnAddTransaction = new javax.swing.JButton();
        btnDeleteTransaction = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        dchrDate = new com.toedter.calendar.JDateChooser();
        spnTime = new javax.swing.JSpinner();

        jLabel1.setText(org.openide.util.NbBundle.getMessage(pAddTransaction.class, "pAddTransaction.jLabel1.text")); // NOI18N

        cbxPartnerName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tblTransaction.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblTransaction.setCellSelectionEnabled(true);
        tblTransaction.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblTransactionKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblTransaction);

        btnAddTransaction.setText(org.openide.util.NbBundle.getMessage(pAddTransaction.class, "pAddTransaction.btnAddTransaction.text")); // NOI18N
        btnAddTransaction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTransactionActionPerformed(evt);
            }
        });

        btnDeleteTransaction.setText(org.openide.util.NbBundle.getMessage(pAddTransaction.class, "pAddTransaction.btnDeleteTransaction.text")); // NOI18N
        btnDeleteTransaction.setPreferredSize(new java.awt.Dimension(52, 30));
        btnDeleteTransaction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteTransactionActionPerformed(evt);
            }
        });

        jLabel2.setText(org.openide.util.NbBundle.getMessage(pAddTransaction.class, "pAddTransaction.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(6, 6, 6)
                .addComponent(cbxPartnerName, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jLabel2)
                .addGap(1, 1, 1)
                .addComponent(dchrDate, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spnTime, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(269, 269, 269)
                .addComponent(btnDeleteTransaction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddTransaction))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(cbxPartnerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(dchrDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spnTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddTransaction)
                    .addComponent(btnDeleteTransaction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddTransactionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTransactionActionPerformed
        // TODO add your handling code here:
        //AddTransactionTableModel attm = (AddTransactionTableModel)this.tblTransaction.getModel();
        int prevRow = tblTransaction.getRowCount() - 1;
        Object[] obj = { tblTransaction.getValueAt(prevRow, 0),
                         tblTransaction.getValueAt(prevRow, 1),
                         tblTransaction.getValueAt(prevRow, 2),
                         tblTransaction.getValueAt(prevRow, 3)};
        ((AddTransactionTableModel)tblTransaction.getModel()).addRow(obj);
        this.tblTransaction.scrollRectToVisible(this.tblTransaction.getCellRect(this.tblTransaction.getModel().getRowCount(), 0, true));
    }//GEN-LAST:event_btnAddTransactionActionPerformed

    private void tblTransactionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblTransactionKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_tblTransactionKeyReleased

    private void btnDeleteTransactionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteTransactionActionPerformed
        // TODO add your handling code here:
        int row = tblTransaction.getSelectedRow();
        if(row >= 0){
            ((AddTransactionTableModel)tblTransaction.getModel()).deleteRow(row);
        }
    }//GEN-LAST:event_btnDeleteTransactionActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddTransaction;
    private javax.swing.JButton btnDeleteTransaction;
    private javax.swing.JComboBox cbxPartnerName;
    private com.toedter.calendar.JDateChooser dchrDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spnTime;
    private javax.swing.JTable tblTransaction;
    // End of variables declaration//GEN-END:variables

}
